package inits

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"zk3.12/config"
	"zk3.12/glodal"
	"zk3.12/internal/model"
)

func MysqlConfig() {
	var err error
	var appconf = config.Appconf.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", appconf.User, appconf.Password, appconf.Host, appconf.Port, appconf.Database)
	glodal.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Println("数据库连接失败")
		return
	}
	log.Println("数据库连接成功")
	err = glodal.DB.AutoMigrate(&model.User{}, &model.Live{}, &model.LiveRoom{})
	if err != nil {
		log.Println("数据迁移失败")
		return
	}
	log.Println("数据迁移成功")
}
